package com.qiaofang.applet.rest;

import com.alibaba.fastjson.JSON;
import com.qiaofang.applet.common.context.QfContextHolder;
import com.qiaofang.applet.common.dto.DataRequest;
import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.common.dto.PageDataRequest;
import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.common.util.PageUtil;
import com.qiaofang.applet.constants.UpDownFlagConstants;
import com.qiaofang.applet.service.property.domain.Estate;
import com.qiaofang.applet.service.property.domain.EstateAlbumPhoto;
import com.qiaofang.applet.service.property.domain.EstateStory;
import com.qiaofang.applet.service.property.domain.Property;
import com.qiaofang.applet.service.property.service.*;
import com.qiaofang.applet.service.property.service.dto.*;
import com.qiaofang.applet.service.property.service.mapstruct.PropertyMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/api/web/esate")
@Api(tags = "房源：小区模块")
public class EstateController {

    @Autowired
    private EstateService estateService;

    @Autowired
    private EstateDetailService estateDetailService;

    @Autowired
    private PropertyService propertyService;

    @Autowired
    private PropertyMapper propertyMapper;

    @Autowired
    private EstateAlbumPhotoService estateAlbumPhotoService;

    @PostMapping("/searchEstate")
    @ApiOperation(value = "查询小区列表", notes = "查询小区列表")
    public DataResponse<Page<EstateListDto>> searchEstate(@RequestBody @Valid PageDataRequest<EstateQueryDto> request) {
        if (StringUtils.isBlank(request.getParam().getCityCode())) {
            throw new BusinessException("请先选择城市");
        }
        log.info("searchEstate request:{}", JSON.toJSONString(request));
        Page<Estate> allEstateByParams = estateService.findAllEstateByParams(request.getParam(), PageUtil.getPageable(request));
        Page<EstateListDto> estateListDtos = estateService.convertDomainToEstateListDto(allEstateByParams, request.getParam().getCityCode());
        return DataResponse.ok(estateListDtos);
    }

    @PostMapping("/showEstateDetail")
    @ApiOperation(value = "查询详情", notes = "查询详情")
    public DataResponse<EstateShowDto> showEstateDetail(@RequestBody @Valid DataRequest<EstateDetailsVersionDto> request) {
        EstateShowDto estateShowDto = estateDetailService.getEstateShowDto(request.getParam().getEstateUniqueUuid(), request.getParam().getVersion());
        //百度地图坐标系转换成腾讯坐标系，仅限小程序端
        if (estateShowDto.getLat() != null && estateShowDto.getLng() != null) {
            double x_pi = 3.14159265358979324 * 3000.0 / 180.0;
            double x = estateShowDto.getLng() - 0.0065;
            double y = estateShowDto.getLat() - 0.006;
            double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
            double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
            estateShowDto.setLng(z * Math.cos(theta));
            estateShowDto.setLat(z * Math.sin(theta));
        }
        return DataResponse.ok(estateShowDto);
    }

    @PostMapping("/showEstateAlbum")
    @ApiOperation(value = "查询小区相册", notes = "查询小区相册")
    public DataResponse<Map<String, List<EstateAlbumPhotoDto>>> showEstateAlbum(@RequestBody @Valid DataRequest<EstateDetailsQueryDto> request) {
        //该楼盘下所有照片类目
        List<EstateAlbumDto> albumPhotos = estateAlbumPhotoService.getAlbums(request.getParam().getEstateUniqueUuid());
        //根据类目 归类 该类目下的照片
        Map<String, List<EstateAlbumPhotoDto>> miniAlbumPhotos = estateAlbumPhotoService.getMiniAlbumPhotos(albumPhotos);

        return DataResponse.ok(miniAlbumPhotos);
    }

    @PostMapping("/showEstateAlbumV2")
    @ApiOperation(value = "查询小区相册V2", notes = "查询小区相册V2")
    public DataResponse<Map<String, List<EstateAlbumPhotoDto>>> showEstateAlbumV2(@RequestBody @Valid DataRequest<EstatePhotoQueryDto> request) {
        return DataResponse.ok(estateAlbumPhotoService.getAlbumPhotosByAlbumId(request.getParam()));
    }


    @PostMapping("/getEstateRent")
    @ApiOperation(value = "小区详情出租", notes = "小区详情出租")
    public DataResponse<Page<RentPropertyListDto>> getEstateRent(@RequestBody @Valid PageDataRequest<EstateDetailQueryRentDto> request) {
        RentPropertyQueryDto rentPropertyQueryDto = new RentPropertyQueryDto();
        BeanUtils.copyProperties(request.getParam(),rentPropertyQueryDto);
        //上架
        rentPropertyQueryDto.setUpDownFlag(UpDownFlagConstants.UP_FLAG.getValue());
        //商圈
        rentPropertyQueryDto.setAreaUuid(request.getParam().getBusinessCircleUuid());
        //区域
        rentPropertyQueryDto.setDistrictUuid(request.getParam().getAreaUuid());
        rentPropertyQueryDto.setCityCode(StringUtils.defaultString(request.getParam().getCityCode(), QfContextHolder.getContext().getCityCode()));
        Page<Property> result = propertyService.findAllRentByParams(rentPropertyQueryDto, PageUtil.getPageable(request));
        Page<RentPropertyListDto> rentPropertyListDtos = propertyMapper.convertDomainToRentListDto(result);
        return DataResponse.ok(rentPropertyListDtos);
    }

    @PostMapping("/getEstateSale")
    @ApiOperation(value = "小区详情出售", notes = "小区详情出售")
    public DataResponse<Page<SalePropertyListDto>> getEstateSale(@RequestBody @Valid PageDataRequest<EstateDetailQuerySaleDto> request) {
        SalePropertyQueryDto salePropertyQueryDto = new SalePropertyQueryDto();
        BeanUtils.copyProperties(request.getParam(),salePropertyQueryDto);
        //上架
        salePropertyQueryDto.setUpDownFlag(UpDownFlagConstants.UP_FLAG.getValue());
        //商圈
        salePropertyQueryDto.setAreaUuid(request.getParam().getBusinessCircleUuid());
        //区域
        salePropertyQueryDto.setDistrictUuid(request.getParam().getAreaUuid());
        salePropertyQueryDto.setCityCode(StringUtils.defaultString(request.getParam().getCityCode(), QfContextHolder.getContext().getCityCode()));
        Page<Property> result = propertyService.findAllSaleByParams(salePropertyQueryDto, PageUtil.getPageable(request));
        Page<SalePropertyListDto> salePropertyListDtos = propertyMapper.convertDomainToSaleListDto(result);
        return DataResponse.ok(salePropertyListDtos);
    }

    @PostMapping("/getEstateComment")
    @ApiOperation(value = "小区业主点评", notes = "小区业主点评")
    public DataResponse<List<EstateCommentDto>> getEstateComment(@RequestBody @Valid DataRequest<EstateDetailsQueryDto> request) {
        return DataResponse.ok(estateDetailService.getEstateComment(request.getParam().getEstateUniqueUuid()));
    }
}